package com.danny.demo.entity;

import com.danny.demo.entity.User;

import java.io.*;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CreateObject4Way
 * @Description: TODO
 * @Author: zhangdong
 * @Date: Created in 9:24 2019/6/22
 * @Version:
 */
public class CreateObject4Way {
    public static void main(String[] args) throws Exception {
        //通过new关键字创建对象
        User user = new User();
        user.setUserName("Danny");
        System.out.println(user.getUserName());

        //通过反射，调用Class类或者Constructor类的newInstance()方法
        //java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例,返回一个布尔值
        Class clazz = Class.forName("com.danny.demo.entity.User");
        Object obj = clazz.newInstance();
        if(obj instanceof User){
            User user1 = (User)obj;
            user1.setUserName("Bob");
            System.out.println(user1.getUserName());
        }
        Constructor<?> constructor = clazz.getConstructor();
        Object obj1 = constructor.newInstance();
        if(obj1 instanceof User){
            User user1 = (User)obj;
            user1.setUserName("Jetty");
            System.out.println(user1.getUserName());
        }
        //通过对象的clone()方法
        User user2 = new User("Flink","10","女","read");
        User user3 = (User)user2.clone();
        System.out.println(user3.getUserName());
        //通过反序列化

    }
}
